<template>
    <div class="ace-code">
        <div>
            <div class="code_tool_bar">
                <div class="code-title">
                    <span>FunCode代码编辑器</span>
                </div>
                <div>
                    <el-select v-model="selectLanguageValue" @change="changSelectCode" class="margin-lr">
                        <el-option label="C++" value="c_cpp"></el-option>
                        <el-option label="C" value="c_cpp"></el-option>
                        <el-option label="Java" value="java"></el-option>
                        <el-option label="Golang" value="golang"></el-option>
                        <el-option label="Python" value="python"></el-option>
                        <el-option label="Javascript" value="javascript"></el-option>
                    </el-select>
                    <el-button icon="el-icon-s-tools" @click="dialogTableVisible = true" class="margin-lr"></el-button>
                    <el-button icon="el-icon-refresh" @click="refreshCode" class="margin-lr"></el-button>
                </div>
            </div>
            <div ref="ace" class="ace"></div>
            <div class="sub-btn">
                <el-button round type="primary" size="medium">提交代码</el-button>
                <el-button round type="success" @click="debuggerCode" size="medium">调试代码</el-button>
            </div>
        </div>
        <el-dialog :visible.sync="dialogTableVisible" title="代码编辑器设置" :append-to-body="true" width="39.194%"
            :destroy-on-close="true">
            <div class="row">
                <el-row>
                    <el-col :span="16">
                        <div class="code-editor-option-title">主题</div>
                        <div class="code-editor-option-description">对白色界面感到厌倦了吗？可以尝试其他的背景和代码高亮风格。</div>
                    </el-col>
                    <el-col :span="8">
                        <el-select v-model="selectThemeValue" @change="changSelectTheme">
                            <el-option label="CrimsonEditor" value="crimson_editor"></el-option>
                            <el-option label="Monokai" value="monokai"></el-option>
                            <el-option label="Terminal" value="terminal"></el-option>
                            <el-option label="Xcode" value="xcode"></el-option>
                        </el-select>
                    </el-col>
                </el-row>
                <hr>
            </div>
            <div class="row">
                <el-row>
                    <el-col :span="16">
                        <div class="code-editor-option-title">编辑类型</div>
                        <div class="code-editor-option-description">更喜欢Vim或者Emacs的输入方式吗？我们也为你提供了这些选项。</div>
                    </el-col>
                    <el-col :span="8">
                        <el-select v-model="selectEditorValue" @change="setEditorMode">
                            <el-option label="Standard" value="vscode"></el-option>
                            <el-option label="Vim" value="vim"></el-option>
                            <el-option label="Emacs" value="emacs"></el-option>
                        </el-select>
                    </el-col>
                </el-row>
                <hr>
            </div>
            <div class="row">
                <el-row>
                    <el-col :span="16">
                        <div class="code-editor-option-title">缩进长度</div>
                        <div class="code-editor-option-description">选择代码缩进的长度。默认是4个空格。</div>
                    </el-col>
                    <el-col :span="8">
                        <el-select v-model="selectTabValue" @change="setTabSize">
                            <el-option label="2个空格" value="2"></el-option>
                            <el-option label="3个空格" value="3"></el-option>
                            <el-option label="4个空格" value="4"></el-option>
                        </el-select>
                    </el-col>
                </el-row>
                <hr>
            </div>
            <div class="dialog-btn">
                <el-button @click="dialogTableVisible = false">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import ace from 'ace-builds'
import 'ace-builds/webpack-resolver'; // 在 webpack 环境中使用必须要导入
//解决添加提示时控制台警告(提示必须引入的包)
import "ace-builds/src-noconflict/ext-language_tools"
import "ace-builds/src-noconflict/ext-emmet"
//语言提示
import 'ace-builds/src-noconflict/snippets/javascript'
import 'ace-builds/src-noconflict/snippets/c_cpp'
import 'ace-builds/src-noconflict/snippets/java'
import 'ace-builds/src-noconflict/snippets/golang'
import 'ace-builds/src-noconflict/snippets/python'
//输入类型
import 'ace-builds/src-noconflict/keybinding-emacs'
import 'ace-builds/src-noconflict/keybinding-vim'
import 'ace-builds/src-noconflict/keybinding-vscode'

export default {
    name: 'CodeEditor',
    props: {
        value: {
            type: String,
            required: true
        }
    },
    methods: {
        changSelectTheme(value) {
            this.aceEditor.setTheme(`ace/theme/${value}`);
        },
        refreshCode() {
            this.aceEditor.session.setValue('');
        },
        changSelectCode(value) {
            this.aceEditor.session.setMode(`ace/mode/${value}`);
        },
        debuggerCode() {
            let value = this.aceEditor.session.getValue();
            console.log(value);
        },
        setTabSize(size) {
            this.aceEditor.session.setTabSize(size);
        },
        setEditorMode(value) {
            this.aceEditor.setKeyboardHandler(`ace/keyboard/${value}`);
        }
    },
    data() {
        return {
            dialogTableVisible: false,
            themeValue: 'ambiance',
            aceEditor: null,
            themePath: 'ace/theme/crimson_editor',
            modePath: 'ace/mode/c_cpp',
            codeValue: this.value || '',
            selectEditorValue: 'Standard',
            selectTabValue: '4个空格',
            selectThemeValue: 'Monokai',
            selectLanguageValue: 'C++'
        };
    },
    mounted() {
        this.aceEditor = ace.edit(this.$refs.ace, {
            maxLines: 1000, // 最大行数，超过会自动出现滚动条
            minLines: 20, // 最小行数，还未到最大行数时，编辑器会自动伸缩大小
            fontSize: 16, // 编辑器内字体大小
            theme: this.themePath, // 默认设置的主题
            mode: this.modePath, // 默认设置的语言模式
            tabSize: 4, // 制表符设置为 4 个空格大小
            readOnly: false, //只读
            highlightActiveLine: true,
            value: this.codeValue
        });
        this.aceEditor.setOptions({
            enableSnippets: true,
            enableLiveAutocompletion: true,
            enableBasicAutocompletion: true
        });
        // 快捷键
        // this.aceEditor.commands.addCommand({
        //   name: 'myCommand',
        //   bindKey: {win: 'Ctrl-M',  mac: 'Command-M'},
        //   exec: function(editor) {
        //     //...
        //   },
        //   readOnly: true // false if this command should not apply in readOnly mode
        // });
    },
    watch: {
        value(newVal) {
            this.aceEditor.setValue(newVal);
        }
    }
};
</script>
  
<style scoped>
.code_tool_bar {
    height: 60px;
    width: calc(100% - 2px);
    background: #f8f9fa;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-top: 1px solid #c2c7d0;
    border-left: 1px solid #c2c7d0;
    border-right: 1px solid #c2c7d0;
}

.code-editor-option-title {
    font-size: 17px;
    margin-bottom: 10px;
}

.code-editor-option-description {
    font-size: 13px;
    color: grey;
}

hr {
    background: none !important;
    height: 1px !important;
    border: 0 !important;
    border-top: 1px solid #eee !important;
    margin-top: 20px;
    margin-bottom: 20px;
}

.ace {
    position: relative !important;
    border: 1px solid lightgray;
    margin: auto;
    height: auto;
    width: 100%;

}

.margin-lr {
    margin-left: 10px;
    margin-right: 10px;
}

.code-title {
    font-size: 17px;
    padding: 0 10px;
    font-family: "HarmonyOS_Sans_Regular";
}

.dialog-btn {
    text-align: right;
}

.sub-btn {
    line-height: 60px;
    height: 60px;
    text-align: right;
}
</style>
